package com.jlnk.androidfmc.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.entity.Message;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private final int CHAT_ME = 100;
  private final int CHAT_YOU = 200;

  private List<Message> items = new ArrayList<>();

  private Context ctx;
  private OnItemClickListener mOnItemClickListener;

  public interface OnItemClickListener {

    void onItemClick(View view, Message obj, int position);
  }

  public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mOnItemClickListener = mItemClickListener;
  }

  public ChatAdapter(Context context) {
    ctx = context;
  }

  public class ItemViewHolder extends RecyclerView.ViewHolder {

    public TextView text_content;
    public TextView text_time;
    public View lyt_parent;
    public View lyt_read;

    public ItemViewHolder(View v) {
      super(v);
      text_content = v.findViewById(R.id.text_content);
      text_time = v.findViewById(R.id.text_time);
      lyt_parent = v.findViewById(R.id.lyt_parent);
      lyt_read = v.findViewById(R.id.lyt_read);
    }
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder vh;
    View v;
    if (viewType == CHAT_ME) {
      v = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.item_chat_me, parent, false);
    } else {
      v = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.item_chat_you, parent, false);
    }
    vh = new ItemViewHolder(v);
    return vh;
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder,
      @SuppressLint("RecyclerView") final int position) {
    if (holder instanceof ItemViewHolder) {
      final Message m = items.get(position);
      ItemViewHolder vItem = (ItemViewHolder) holder;
      vItem.text_content.setText(m.getContent());
      vItem.text_time.setText(m.getDate());
      vItem.text_time.setVisibility(m.isShowTime() ? View.VISIBLE : View.GONE);
      if (position == getItemCount() - 1 && m.isFromMe()) {
        vItem.lyt_read.setVisibility(View.VISIBLE);
      } else {
        vItem.lyt_read.setVisibility(View.INVISIBLE);
      }
      vItem.lyt_parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(view, m, position);
          }
        }
      });
    }
  }

  @Override
  public int getItemCount() {
    return items.size();
  }

  @Override
  public int getItemViewType(int position) {
    return this.items.get(position).isFromMe() ? CHAT_ME : CHAT_YOU;
  }

  public void insertItem(Message item) {
    this.items.add(item);
    notifyItemInserted(getItemCount());
    if (getItemCount() > 1) {
      notifyItemChanged(getItemCount() - 2);
    }
  }

  public void setItems(List<Message> items) {
    this.items = items;
  }
}